package com.gt.platform.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gt.common.HttpUtils;
import com.gt.platform.domain.ApplyReqInfoVo;
import com.gt.platform.domain.Data;
import com.gt.platform.domain.ReportParam;
import com.gt.platform.domain.Result;
import com.gt.platform.mapper.CmdSendHisMapper;
import com.gt.platform.service.PlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@Api(tags = "平台接口")
@Slf4j
@RestController
@RequestMapping("/platform")
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    @Autowired
    private CmdSendHisMapper cmdSendHisMapper;

    /**
     * 7.64查询检查申请列表 S5001
     * 7.65查询检查申请详情 S5002
     *
     * @param applyReqInfoVo
     * @return
     */
    @ApiOperation("获取申请单")
    @PostMapping("selectCheckApplyList")
    public Result selectCheckApplyList(@RequestBody ApplyReqInfoVo applyReqInfoVo) {
        long start = System.currentTimeMillis();
        String retResult = platformService.selectCheckApplyList(applyReqInfoVo);
        long time =  System.currentTimeMillis() - start;
        log.info("调用平台消耗时间【{}】",time);
        Result<Data> result = new Result<>();
        ArrayList<Data> dataList = new ArrayList<>();
        Data data = new Data();
        if (retResult.equals("true")) {
            data.setFlag("1");
            data.setErrorMsg("获取成功");
            dataList.add(data);
            result.setCode("200");
            result.setMsg("获取成功");
            result.setData(dataList);
            return result;
        }

        data.setFlag("0");
        data.setErrorMsg(retResult);
        dataList.add(data);
        result.setCode("500");
        result.setMsg("获取失败");
        result.setData(dataList);
        return result;
    }



    @ApiOperation("测试接口")
    @GetMapping("interfaceTest")
    public void interfaceTest()  {
        //cmdSendHisMapper.updateSendFlag("04F58D0C-6A35-46B9-BE73-690C5D0A5393","0");
    }




//    /**
//     * 已丢弃使用
//     *
//     * @param jsonString
//     * @return
//     * @throws Exception
//     */
//    @ApiOperation("获取申请单")
//    @GetMapping("selectData")
//    public AjaxResult selectData(String jsonString) throws Exception {
//
//        ObjectMapper mapper = new ObjectMapper();
//        RecApplyInfo input = mapper.readValue(jsonString, RecApplyInfo.class);
//
//        XmlRoot xmlRoot = new XmlRoot();
//        XmlInput xmlInput = new XmlInput();
//
//        XmlAckInf xmlAckInf = new XmlAckInf();
//        AckInfo ackInfo = input.getInput().getAck_info();
//        BeanUtils.copyProperties(ackInfo, xmlAckInf);
//
//        XmlPv1Info xmlPv1Info = new XmlPv1Info();
//        Pv1Info pv1Info = input.getInput().getPv1_info().get(0);
//        BeanUtils.copyProperties(pv1Info, xmlPv1Info);
//
//        XmlApplyInfo xmlApplyInfo = new XmlApplyInfo();
//        ApplyInfo applyInfo = input.getInput().getApply_info().get(0);
//        BeanUtils.copyProperties(applyInfo, xmlApplyInfo);
//
//        //todo fee_item
//        //todo head
//        //todo addition_item
//        XmlPartInfo xmlPartInfo = new XmlPartInfo();
//        PartInfo partInfo = input.getInput().getApply_info().get(0).getPart_info().get(0);
//        BeanUtils.copyProperties(partInfo, xmlPartInfo);
//
//        xmlRoot.setInput(xmlInput);
//        xmlRoot.getInput().setAck_info(xmlAckInf);
//        xmlRoot.getInput().setPv1_info(xmlPv1Info);
//        xmlRoot.getInput().setApply_info(xmlApplyInfo);
//        xmlRoot.getInput().setPart_info(xmlPartInfo);
//
//        String json = JSON.toJSON(xmlRoot).toString();
//        String xml = JsonToXmlConverter.convertToXml(json);
//        String xmlResult = xml.replace("<ObjectNode>", "<Root>").replace("</ObjectNode>", "</root>");
//
//        String applyId = "0";
//        selectApplyDetail(xmlRoot, applyId);
//
//        return AjaxResult.success(xmlResult);
//    }

//    private void selectApplyDetail(XmlRoot xmlRoot, String applyId) throws JsonProcessingException {
//        ReqParamDetail reqParamDetail = new ReqParamDetail();
//        InPutDetail inPutDetail = new InPutDetail();
//        ApplyReqInfoDetail applyReqInfoDetail = new ApplyReqInfoDetail();
//
//        applyReqInfoDetail.setApply_id(applyId);
//        inPutDetail.setReq_info(applyReqInfoDetail);
//        reqParamDetail.setInput(inPutDetail);
//
//        String jsonString = JSON.toJSONString(reqParamDetail);
//        log.info("明細的參數：{}", jsonString);
////        jsonString = stringcreate();
//        ObjectMapper objectMapper = new ObjectMapper();
//        JsonNode rootNode = objectMapper.readTree(applyId);
//
//        JsonNode input = rootNode.get("input");
//        JsonNode applyInfos = input.get("apply_info");
//        List<XmlAdditionItem> xmlAdditionItems = new ArrayList();
//        for (JsonNode applyInfo : applyInfos) {
//            JsonNode additionItems = applyInfo.get("addition_item");
//            for (JsonNode additionItem : additionItems) {
//                XmlAdditionItem xmlAdditionItem = new XmlAdditionItem();
//                String itemName = additionItem.get("item_name").toString();
//                String itemContent = additionItem.get("item_content").toString();
//                xmlAdditionItem.setItem_name(itemName);
//                xmlAdditionItem.setItem_content(itemContent);
//                xmlAdditionItems.add(xmlAdditionItem);
//            }
//            break;
//        }
//
//        JsonNode feeItems = input.get("fee_item");
//        XmlFeeItem xmlFeeItem = new XmlFeeItem();
//        List<XmlFeeItem> xmlFeeItems = new ArrayList<>();
//        for (JsonNode feeItem : feeItems) {
//            xmlFeeItem.setReceipt_no(feeItem.get("receipt_no").toString());
//            xmlFeeItem.setFees_item(feeItem.get("fees_item").toString());
//            xmlFeeItem.setFee_item_id(feeItem.get("fee_item_id").toString());
//            xmlFeeItem.setFee_item_name(feeItem.get("fee_item_name").toString());
//            xmlFeeItem.setGjmlbm(feeItem.get("gjmlbm").toString());
//            xmlFeeItem.setFee_item_unit(feeItem.get("fee_item_unit").toString());
//            xmlFeeItem.setFee_count(feeItem.get("fee_count").toString());
//            xmlFeeItem.setShould_money(feeItem.get("should_money").toString());
//            xmlFeeItem.setActual_money(feeItem.get("actual_money").toString());
//            xmlFeeItem.setApply_dept_id(feeItem.get("apply_dept_id").toString());
//            xmlFeeItem.setApply_dept_name(feeItem.get("apply_dept_name").toString());
//            xmlFeeItem.setExe_dept_id(feeItem.get("exe_dept_id").toString());
//            xmlFeeItem.setExe_dept_name(feeItem.get("exe_dept_name").toString());
//            xmlFeeItem.setReceipt_time(feeItem.get("receipt_time").toString());
//            xmlFeeItem.setPlacer_name(feeItem.get("placer_name").toString());
//            xmlFeeItem.setCitem_id(feeItem.get("citem_id").toString());
//            xmlFeeItem.setCitem_name(feeItem.get("citem_name").toString());
//            xmlFeeItem.setOrder_id(feeItem.get("order_id").toString());
//            xmlFeeItem.setApply_id(feeItem.get("apply_id").toString());
//            xmlFeeItem.setCharge_status(feeItem.get("charge_status").toString());
//            xmlFeeItems.add(xmlFeeItem);
//        }
//
//        xmlRoot.getInput().setFee_item(xmlFeeItems);
//        xmlRoot.getInput().setAddition_item(xmlAdditionItems);
//    }


}
